package com.jingyanzi.marketing.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.ActivityMapper;
import com.jingyanzi.marketing.domain.ActivityDo;
import com.jingyanzi.marketing.entity.MaActivity;
import com.jingyanzi.marketing.entity.MaActivityExample;
import com.jingyanzi.marketing.entity.MaActivityExample.Criteria;
import com.jingyanzi.marketing.query.ActivityQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IActivityService;


@Service("maActivityService")
public class ActivityServiceImpl extends BaseServiceImpl implements IActivityService {

	@Autowired
	private ActivityMapper maActivityMapper;

	public ResultDO add(ActivityDo maActivityDo) {
		MaActivity activity = new MaActivity();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		DateFormat dateForm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		activity.setName(maActivityDo.getName());
		activity.setContent(maActivityDo.getContent());
		activity.setAuthor(maActivityDo.getAuthor());
		activity.setType(maActivityDo.getType());
		activity.setUseCount(maActivityDo.getUseCount());
		activity.setUserType(maActivityDo.getUserType());
		activity.setStatus(maActivityDo.getStatus());
		activity.setRegionCode(StringUtils.join(maActivityDo.getRegionCode(), ","));
		activity.setRegion(StringUtils.join(maActivityDo.getRegion(), ","));
		try {
			String activityEndDate = dateForm.format(Long.valueOf(maActivityDo.getActivityEndDate()));
			String activityDate = dateForm.format(Long.valueOf(maActivityDo.getActivityDate()));
			activity.setActivityEndDate(dateForm.parse(activityEndDate));
			activity.setActivityDate(dateForm.parse(activityDate));
		} catch (ParseException e1) {
			e1.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		activity.setActivityCount(maActivityDo.getActivityCount());
		activity.setCreatetime(Calendar.getInstance().getTime());
		activity.setIsDelete(1);

		int r = -1;

		try {
			r = maActivityMapper.insert(activity);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, activity.getId());
		return result;
	}

	public ResultDO modifi(ActivityDo maActivityDo) {
		MaActivity activity = new MaActivity();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		DateFormat dateForm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		activity.setId(maActivityDo.getId());
		activity.setName(maActivityDo.getName());
		activity.setStatus(maActivityDo.getStatus());
		activity.setContent(maActivityDo.getContent());
		activity.setModifi(maActivityDo.getModifi());
		activity.setType(maActivityDo.getType());
		activity.setUseCount(maActivityDo.getUseCount());
		activity.setUserType(maActivityDo.getUserType());
		activity.setRegionCode(StringUtils.join(maActivityDo.getRegionCode(), ","));
		activity.setRegion(StringUtils.join(maActivityDo.getRegion(), ","));
		try {
			if (maActivityDo.getActivityEndDate() != null) {
				String activityEndDate = dateForm.format(Long.valueOf(maActivityDo.getActivityEndDate()));
				String activityDate = dateForm.format(Long.valueOf(maActivityDo.getActivityDate()));
				activity.setActivityEndDate(dateForm.parse(activityEndDate));
				activity.setActivityDate(dateForm.parse(activityDate));
			}
		} catch (ParseException e1) {
			e1.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		activity.setActivityCount(maActivityDo.getActivityCount());
		int r = -1;

		try {
			r = maActivityMapper.updateByPrimaryKeySelective(activity);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO remove(Integer id) {
		MaActivity activity = new MaActivity();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		activity.setId(id);
		activity.setIsDelete(-1);
		int r = -1;

		try {
			r = maActivityMapper.updateByPrimaryKeySelective(activity);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO get(Integer id) {
		MaActivity activity = new MaActivity();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			activity = maActivityMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		if (activity == null) {
			result.setErrorCode(ResultDO.ACTIVITY_GET_ERROR);
			result.setErrorMsg("不存在该优惠活动");
			result.setSuccess(false);
			return result;
		}
		ActivityDo activityDo = getMaActivityDo(activity);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, activityDo);
		return result;
	}

	public ResultDO query(ActivityQuery maActivityQuery) {
		ResultSupport result = new ResultSupport();
		List<MaActivity> list = null;

		MaActivityExample example = new MaActivityExample();
		Criteria createCriteria = example.createCriteria();
		
		if(StringUtils.isNotEmpty(maActivityQuery.getStartTimeStamp()) &&
				StringUtils.isNotEmpty(maActivityQuery.getEndTimeStamp())) {
			Date startTime = new Date((Long.parseLong(maActivityQuery.getStartTimeStamp()) - (24 * 60 * 60 * 1000)));
			Date endTime = new Date((Long.parseLong(maActivityQuery.getEndTimeStamp()) + (24 * 60 * 60 * 1000)));
			System.out.println(startTime + ":" + endTime);
			createCriteria.andActivityDateBetween(startTime, endTime);
		}
		
		if (StringUtils.isNotBlank(maActivityQuery.getName())) {
			createCriteria.andNameLike("%" + maActivityQuery.getName() + "%");
		}

	
		createCriteria.andTypeEqualTo(maActivityQuery.getType());
		
		if (maActivityQuery.getUserType() != null) {
			createCriteria.andUserTypeEqualTo(maActivityQuery.getUserType());
		}
		createCriteria.andActivityEndDateGreaterThan(Calendar.getInstance().getTime());

		createCriteria.andIsDeleteEqualTo(1);

		List<ActivityDo> maActivityDos = null;

		if (maActivityQuery.isPage()) {
			example.setLimitByPage(maActivityQuery.getStart() + "," + maActivityQuery.getPageRows());
			long count = maActivityMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = maActivityMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取优惠活动失败");
			result.setSuccess(false);
			return result;
		}

		maActivityDos = getMaActivityDoList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, maActivityDos);

		return result;
	}
}
